//
//  SJGetCodeVC.m
//  SJPay
//
//  Created by 凯朱 on 2024/11/26.
//

#import "SJGetCodeVC.h"
#import "CRBoxInputView.h"
#import "CRLineView.h"
@interface SJGetCodeVC (){
    NSString *_code;
}

@end

@implementation SJGetCodeVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tipLbl1.text = kLocalString(@"请输入短信验证码");
    _tipLbl2.text = [NSString stringWithFormat:@"%@：%@",kLocalString(@"验证码已发送至"),_tel];
    [_loginBtn setTitle:kLocalString(@"登录") forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:kHexColor(@"#5E81FF")] forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:kHexColor(@"#C6C6C6")] forState:UIControlStateDisabled];
    _loginBtn.layer.cornerRadius = 25;
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.enabled = NO;
    [_resendBtn startOverTime:60];
   
    [self addCodePutView];
}

-(void)addCodePutView{
    CRBoxInputCellProperty *cellProperty = [CRBoxInputCellProperty new];
    cellProperty.cellFont = kFontMedium(20);
    cellProperty.cellTextColor = KColor_1;
    cellProperty.cellBorderColorFilled = KClearColor;
    cellProperty.cellBorderColorNormal = KClearColor;
    cellProperty.cellBorderColorSelected = KClearColor;
    cellProperty.cellCursorColor = KColor_1;
    cellProperty.showLine = YES; //必需
    [cellProperty setCustomLineViewBlock:^CRLineView * _Nonnull{
        CRLineView *lineView = [CRLineView new];
        lineView.underlineColorNormal = kHexColor(@"#DCDCDC");
        lineView.underlineColorSelected = KColor_B;
        lineView.underlineColorFilled = KColor_B;
        [lineView.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(2);
            make.left.right.bottom.offset(0);
        }];

        lineView.selectChangeBlock = ^(CRLineView * _Nonnull lineView, BOOL selected) {
            if (selected) {
                [lineView.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(2);
                }];
            } else {
                [lineView.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(2);
                }];
            }
        };
        return lineView;
    }];
    
   
  
    CRBoxInputView *boxInputView = [[CRBoxInputView alloc] initWithCodeLength:4];
    boxInputView.customCellProperty = cellProperty;
    [boxInputView loadAndPrepareViewWithBeginEdit:YES];
    boxInputView.frame = CGRectMake(40, 0, kScreenWidth-80, _codeView.height);
    boxInputView.textDidChangeblock = ^(NSString *text, BOOL isFinished) {
        self->_code = text;
        self->_loginBtn.enabled = isFinished;
    };
    [_codeView addSubview:boxInputView];
}

- (IBAction)loginAction:(id)sender {
    NSDictionary *params = @{@"phone":_tel,
                             @"captcha":_code,
                             @"register_lat":@"",
                             @"register_lng":@"",
                             @"brand":@"",
                             @"rom":@"",
                             @"ram":@""};
    [ZCTool showHudOnView:self.view];
    [ZKHttpManager postWithPath:@"login/smsloginsaveT" params:params finished:^(BOOL isSuccessed, id result, NSString *code, NSString *message) {
        [ZCTool hideHudOnView:self.view];
        if (!isSuccessed) {
            kShowToast(message);
            return;
        }
        NSDictionary *data = result[@"data"];
        [[UserManager sharedUserManager] saveUserInfo:data];
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginStatusChange" object:nil];
        MainTabBarController *tab = [MainTabBarController new];
        [UIApplication sharedApplication].keyWindow.rootViewController = tab;
    }];
}



- (IBAction)resendAction:(id)sender {
    [_resendBtn startOverTime:5];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



@end
